#include <stdio.h>
#include <string.h>
#include "nvs_flash.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "usb_config.h"
#include "driver/usb_serial_jtag.h"
#include "sdkconfig.h"
#include "esp_log.h"
#include "esp_check.h"
#include "usb2uart.h"
#include "usbd_core.h"
#include "usbd_cdc.h"
#include "dap_setting.h"
#include "esp_jtag_main.h"
#include "work_status_setting.h"
#include "dap_main.h"
#include "dap_usb_handle.h"
#include "mdns_handle.h"
#include "wifi_main.h"
#include "tcp_server.h"
#include "logic_analyzer_sump.h"
#include "esp_psram.h" // PSRAM支持
#include "ir_air_conditioner.h" // PSRAM支持
#include "pin_config.h"
#include "hal_i2c.h"
#include "pca9557pw.h"
#include "lcd.h"
#include "start_ui.h"
#include "audio.h"
#include "ui_setting.h"
#include "custom_nvs.h"
#include "dap_setting.h"
#include "app_sr.h"
#include "md_shell.h"

/*pyocd flash -t stm32f429xg --prob 00000000000000000123456789ABCDEF --frequency 80000000 --format elf smarthome_demo.axf


 pyocd cmd -t stm32f429xg -O interface=jtag -O connect_mode=under-reset
*/
#define MAIN_PROGRAM 1
void app_main()
{

#ifdef WIRELESS_ENABLE
    // Initialize NVS
    esp_err_t ret = nvs_flash_init();
    if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
        ESP_ERROR_CHECK(nvs_flash_erase());
        ret = nvs_flash_init();
    }
    wifi_init();//初始化wifi, 连接wifi
    mdns_setup();
    // set_work_status(ESP_JTAG_WORK);
    // esp_jtag_init(0, ESP_USBD_BASE);

    /*tcp*/

    set_work_status(DAP_WORK);
    load_dap_setting();
    DAP_Setup();
    // dap_usb_init(0, ESP_USBD_BASE);
    //DAP_Pubilc_Init();
    // Specify the usbip server task
#if (USE_TCP_NETCONN == 1)
        xTaskCreatePinnedToCore(tcp_netconn_task, "tcp_server", 4096, NULL, 14, NULL, DAP_TASK_AFFINITY);
#else // BSD style
        xTaskCreatePinnedToCore(tcp_server_task, "tcp_server", 4096, NULL, 14, NULL,
                                1);
#endif
#endif //WIRELESS_ENABLE
#ifdef AIR_ENABLE
            //    // ESP_LOGI("main", "esp_psram_is_initialized: %d", esp_psram_is_initialized());
            //     set_work_status(LOGIC_WORK);
            //     logic_analyzer_init();
            uint8_t temp;
        pAir_conditioner_t pAC = ir_air_conditioner_init(MIDEA_R05D_PROTOCOL, MDLINK_IR_TX_IO);

        // for(int i=0;i<3;i++)
        // {
        //     int ret=air_conditioner_send(pAC);
        //     vTaskDelay(pdMS_TO_TICKS(100));
        // }
        air_conditioner_send(pAC);
        air_conditioner_get_temp(pAC, &temp);
        ESP_LOGI("main", "air_conditioner_get_temp: %d", temp);
        vTaskDelay(pdMS_TO_TICKS(1000));
        air_conditioner_set_temp(pAC, 18);
        air_conditioner_send(pAC);
        air_conditioner_get_temp(pAC, &temp);
        ESP_LOGI("main", "air_conditioner_get_temp: %d", temp);
        vTaskDelay(pdMS_TO_TICKS(1000));
        air_conditioner_send(pAC);
        vTaskDelay(pdMS_TO_TICKS(1000));

        air_conditioner_set_power(pAC, 0);
        air_conditioner_send(pAC);
        vTaskDelay(pdMS_TO_TICKS(1000));
        air_conditioner_send(pAC);
        air_conditioner_del(pAC);
#endif //AIR_ENABLE

#ifdef MAIN_PROGRAM
        set_work_status(USUALLY_WORK); // Initialize NVS.
        esp_err_t ret = nvs_flash_init();
        if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
            ESP_ERROR_CHECK(nvs_flash_erase());
            ret = nvs_flash_init();
        }
        ESP_ERROR_CHECK(ret);
        custom_nvs_init(); // 初始化自定义NVS

        bsp_i2c_init();     // I2C初始化
        pca9557_init();     // IO扩展芯片初始化
        bsp_lvgl_start();   // 初始化液晶屏lvgl接口
        bsp_codec_init();   // 音频初始化
        load_ui_setting();  // 加载UI配置
        load_dap_setting(); // 加载DAP配置
        // dap_setting_load_default(true);                // 加载默认dap配置
        // ui_setting_load_default(true);                 // 加载默认UI配置
        lv_gui_start(); // 显示开机界面
      // ai_gui_in();
        // is_open_voice_control(); //根据配置决定是否开启语音控制
        xTaskCreatePinnedToCore(power_music_task, "power_music_task", 4 * 1024, NULL, 5, NULL, 1); // 播放开机音乐
#if (CONFIG_ESP_CONSOLE_UART_ENABLE==1)
        md_shell_init(); // 启动shell任务
#endif //CONFIG_ESP_CONSOLE_UART_ENABLE==1
#endif //MAIN_PROGRAM

#ifdef TEST_PROGRAM
    set_work_status(ESP_JTAG_WORK);
    
    esp_jtag_init(0, ESP_USBD_BASE);
#endif //TEST_PROGRAM
}
